<?php

namespace App\Admin\Controllers;

use App\Models\Branch;
use App\Models\Member;
use App\Http\Controllers\Controller;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class MembersController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
        ->header('成员管理')
        ->description('小伙伴们')
        ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('详细信息')
            ->description('他的隐私')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('修改信息')
            ->description('盘它')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('新增信息')
            ->description('无中生有')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $branch=$this->getBranch();
        $grid = new Grid(new Member);
        $grid->model()->where('id', '>', 0);
        $grid->actions(function ($actions) {
            $actions->disableView();
        });
        if($branch=='member'){
            $grid->disableActions();
        }
        if($branch==3||$branch==4||$branch==5||$branch==6){
            $grid->model()->where("branch_id",$branch);
        }
        $grid->tools(function ($tools) {
            $tools->append('<a href="http://api.mtaction.com/dian/Qm/qrcode/qrcode.php?data=http://slave.mtaction.com/mtshare-web/index.html" target="_blank"><button class="btn btn-info btn-sm">生成邀请二维码</button></a>');
        });
//        $grid->filter(function($filter){
//            $filter->disableIdFilter();
//            $filter->like('name', '姓名');
//            $filter->in('state')->radio([
//                0   => '未审核',
//                1   => '已审核',
//            ]);
//        });
        $grid->id('Id');
        $grid->name('姓名');
        $grid->column('branch.name','所在部门');
        $grid->share_count('分享次数');
        $grid->power('内力值');
        $grid->nickname('微信昵称');
        $grid->column('avatar','微信头像')->display(function (){
            return '<img src="'.$this->avatar.'" style="width:70px;height:70px;">';
        });
        $states = [
            'on'  => ['value' => 1, 'text' => '已审核', 'color' => 'success'],
            'off' => ['value' => 0, 'text' => '未审核', 'color' => 'danger'],
        ];
        $grid->state('审核状态')->switch($states);
        $grid->column('他的绩效')->display(function () {
            if($this->branch_id==3||$this->branch_id==4||$this->branch_id==5||$this->branch_id==6) {
                return '<a href="http://192.168.0.200:8099/admin/member/grades?member_id=' . $this->id . '"><button class="btn btn-info btn-sm">详细信息</button></a>';
            }else{
                return "";
            }
        });
        return $grid;
    }
    public function getBranch(){
        $branch="";
        $mess=$_SERVER['REQUEST_URI'];
        if(Admin::user()->isRole('master')){
            if(strpos($mess,'master')){
                $member= Member::all()->toArray();
                foreach ($member as $val){
                    if($val['username']==Admin::user()->username){
                        $branch=$val['branch_id'];
                    }
                }
                return $branch;
            }
        }elseif(Admin::user()->isRole('administrator')){
            if(strpos($mess,'xiaoyao')) {
                return $branch =3;
            }elseif(strpos($mess,'gumu')){
                return $branch =4;
            }elseif(strpos($mess,'wudang')){
                return $branch =5;
            }else{
                return $branch = "明教";
            }
        }elseif (Admin::user()->isRole('member')){
            return $branch='member';
        }
    }
    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Member::findOrFail($id));

        $show->id('Id');
        $show->name('姓名');
        $show->branch('所在部门');
        $show->share_count('分享次数');
        $show->state('审核状态');
        $show->nickname('昵称');
        $show->avatar('头像');
        $show->created_at('Created at');
        $show->updated_at('Updated at');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Member);
        $form->tools(function (Form\Tools $tools) {
            $tools->disableList();
            $tools->disableDelete();
            $tools->disableView();
        });
        $form->footer(function ($footer) {
            $footer->disableReset();
            $footer->disableViewCheck();
            $footer->disableEditingCheck();
            $footer->disableCreatingCheck();
        });
        $form->text('name', '姓名');
        $form->select('branch_id', '所在部门')->options(Branch::all()->pluck('name','id'));
        $form->text('power', '内力值');
        $form->text('username', '账号');
        $form->password('password', trans('admin.password'));
        $form->number('share_count', '分享次数');
        $states = [
            'on'  => ['value' => 1, 'text' => '已审核', 'color' => 'success'],
            'off' => ['value' => 0, 'text' => '未审核', 'color' => 'danger'],
        ];
        $form->switch('state','审核状态')->states($states);
        return $form;
    }
}
